﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using static DXApplicationTangche.原型.FormCheckedListBox;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.订单一览新.辅料.辅料管理;

namespace DXApplicationTangche.UC.订单一览新.辅料.辅料下单
{
    public partial class XtraUserControl辅料选择 : DevExpress.XtraEditors.XtraUserControl
    {
        private Boolean ifStore = true;
        private String materialCode;
        private String accessoryCatalogId;
        private String accessoryCatalogName;
        private String subMaterialCode;
        private String subMaterialName;
        private List<DTO辅料一览> DTO辅料一览s = new List<DTO辅料一览>();
        private List<DTO辅料CheckList> DTO辅料CheckLists = new List<DTO辅料CheckList>();
        //  辅料列表
        private List<StyleAccessoryDTO> StyleAccessoryDTOs = new List<StyleAccessoryDTO>();
        private List<DTO辅料显示> DTO辅料显示s = new List<DTO辅料显示>();
        private DTO辅料一览 dTO辅料一览 = new DTO辅料一览();
        public XtraUserControl辅料选择()
        {
            InitializeComponent();
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
            this.searchLookUpEdit选择辅料.Properties.DataSource = new Model辅料一览(true).DTO辅料一览s;
        }
        public XtraUserControl辅料选择(Boolean ifStore,String materialCode)
        {
            InitializeComponent();
            this.ifStore = ifStore;
            this.materialCode = materialCode;
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(this.ifStore);
            this.searchLookUpEdit选择辅料.Properties.DataSource = new Model辅料一览(true).DTO辅料一览s;
        }
        public XtraUserControl辅料选择(标识StyleAccessoryDTO 标识StyleAccessoryDTO,String materialCode)
        {
            InitializeComponent();
            this.searchLookUpEdit辅料类别.Properties.DataSource = 辅料Service.get辅料类别一览(false);
            this.searchLookUpEdit选择辅料.Properties.DataSource = new Model辅料一览(true).DTO辅料一览s;
            this.ifStore = 标识StyleAccessoryDTO.ifStore;
            this.StyleAccessoryDTOs = 标识StyleAccessoryDTO.styleAccessoryDTOs;
            this.materialCode = materialCode;
            this.页面显示();
        }


        #region 控件事件
        #region 直接选择辅料
        private void searchLookUpEdit选择辅料_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn选择辅料_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择辅料View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择辅料.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.subMaterialCode = this.searchLookUpEdit选择辅料.Properties.View.GetRowCellValue(rowHandle, "subMaterialCode").ToString();
                    this.subMaterialName = this.searchLookUpEdit选择辅料.Properties.View.GetRowCellValue(rowHandle, "subMaterialName").ToString();
                }
            }
            DTO辅料一览 dTO辅料一览 = 辅料Service.getAccessoryByAccessoryCode(this.subMaterialCode);
            new Form辅料属性(this.StyleAccessoryDTOs, dTO辅料一览).ShowDialog();
            this.页面显示();
        }
        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn选择辅料_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择辅料.ToolTip = null;
            searchLookUpEdit选择辅料.EditValue = null;
        }
        private void searchLookUpEdit选择辅料_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.subMaterialName;
        }
        #endregion
        #region 选择辅料类别
        private void searchLookUpEdit辅料类别_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit辅料类别View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit辅料类别.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.accessoryCatalogId = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogId").ToString();
                    this.accessoryCatalogName = this.searchLookUpEdit辅料类别.Properties.View.GetRowCellValue(rowHandle, "accessoryCatalogName").ToString();
                }
            }
            this.DTO辅料一览s = 辅料Service.get辅料CheckList(this.materialCode, this.accessoryCatalogId);
            this.DTO辅料CheckLists.Clear();
            foreach (DTO辅料一览 item in this.DTO辅料一览s)
            {
                this.DTO辅料CheckLists.Add(new DTO辅料CheckList(item));
            }
            this.checkedListBoxControl辅料.DataSource = this.DTO辅料CheckLists;
            this.checkList全不选();
        }
        private void checkList全不选()
        {
            for (int i = 0; i < this.checkedListBoxControl辅料.ItemCount; i++)
            {
                this.checkedListBoxControl辅料.SetItemChecked(i, false);
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料类别.ToolTip = null;
            searchLookUpEdit辅料类别.EditValue = null;
        }
        private void searchLookUpEdit辅料类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.accessoryCatalogName;
        }
        #endregion
        private void checkedListBoxControl辅料_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (this.checkedListBoxControl辅料.GetItemChecked(e.Index))
            {
                DTO辅料CheckList dto = this.checkedListBoxControl辅料.GetItem(e.Index) as DTO辅料CheckList;
                foreach (DTO辅料一览 item in this.DTO辅料一览s)
                {
                    if (dto.code.Equals(item.subMaterialCode))
                    {
                        this.dTO辅料一览 = item;
                        break;
                    }
                }
                new Form辅料属性(this.StyleAccessoryDTOs, this.dTO辅料一览).ShowDialog();
            }
            this.页面显示();
        }
        private void 页面显示()
        {
            this.DTO辅料显示s.Clear();
            foreach (StyleAccessoryDTO item in this.StyleAccessoryDTOs)
            {
                this.DTO辅料显示s.Add(new DTO辅料显示(item));
            }
            this.gridControl辅料.DataSource = this.DTO辅料显示s;
            this.advBandedGridView辅料.RefreshData();
        }

        private void advBandedGridView辅料_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            String subMaterialCode = 
                FunctionHelper.GetAdvBandedgridViewValue(
                    this.advBandedGridView辅料, 
                    this.advBandedGridView辅料.FocusedRowHandle, 
                    "subMaterialCode");
            foreach (StyleAccessoryDTO item in this.StyleAccessoryDTOs)
            {
                if (item.subMaterialCode.Equals(subMaterialCode))
                {
                    new Form辅料属性(
                        item,
                        true
                        ).ShowDialog();
                    break;
                }
            }
            this.页面显示();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (StyleAccessoryDTO item in this.StyleAccessoryDTOs)
            {
                if (item.subMaterialCode.Equals(
                    FunctionHelper.GetAdvBandedgridViewValue(
                        this.advBandedGridView辅料,this.advBandedGridView辅料.FocusedRowHandle,"subMaterialCode")
                    )
                )
                {
                    this.StyleAccessoryDTOs.Remove(item);
                    break;
                }
            }
            this.页面显示();
        }
        #endregion

        public 标识StyleAccessoryDTO build() {
            return new 标识StyleAccessoryDTO(
                this.ifStore,
                this.StyleAccessoryDTOs
                );
        }

        private void textBox辅料编码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.textBox辅料编码.Text.Trim()))
                {
                    DTO辅料一览 dTO辅料一览 = 辅料Service.getAccessoryByAccessoryCode(this.textBox辅料编码.Text.Trim());
                    if (!String.IsNullOrEmpty(dTO辅料一览.subMaterialCode))
                    {
                        new Form辅料属性(this.StyleAccessoryDTOs, dTO辅料一览).ShowDialog();
                        this.页面显示();
                    }
                    else
                    {
                        MessageBox.Show("未查询到辅料");
                    }
                }
                else
                {
                    MessageBox.Show("请输入辅料编码");
                }
                this.textBox辅料编码.Clear();
            }
        }
    }
}
